package dataManage;
import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;

public class dataBaseConnection {
	public static Connection getConnectionLogin(){
		try
		{
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/userlogin");
			Connection conn = ds.getConnection();
			return conn;
		}
		catch(Exception se)
		{
			se.printStackTrace();
			return null;
		}
	}
	public static Connection getConnectionUserData(){
		try
		{
			InitialContext ctx = new InitialContext();
			DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/usersdata");
			Connection conn = ds.getConnection();
			return conn;
		}
		catch(Exception se)
		{
			se.printStackTrace();
			return null;
		}
	}
	public static void closeCon(Connection con){
		try {
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static boolean isUser(String itId,String itPassWord){
		Connection con=dataBaseConnection.getConnectionLogin();
		try{
			PreparedStatement ps=con.prepareStatement("select password from users where name=?");
		    ps.setString(1,itId);
			ResultSet rs=ps.executeQuery();
		    while(rs.next()){
		    	if(itPassWord.equals(rs.getString(1))){
		    		return true;
		    	}	
		    }
		    return false;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally{
			closeCon(con);
		}
	}
	public static boolean register(String itCardId){
		Connection con=dataBaseConnection.getConnectionUserData();
		Connection conLogin=dataBaseConnection.getConnectionLogin();
		String itName=new String();
		String itCollege=new String();
		try{
				PreparedStatement psLogin=conLogin.prepareStatement("select username,usercollege from users where name=?");
				psLogin.setString(1,itCardId);
				ResultSet rs=psLogin.executeQuery();
				while (rs.next()) {
					itName=rs.getString(1);
					itCollege=rs.getString(2);
				}
				PreparedStatement psInsert=con.prepareStatement("insert into userdata(userId,cardId,userName,userCollege) values(?,?,?,?)");
				int itUserId=getUserId();
				psInsert.setInt(1,itUserId);
				psInsert.setString(2,itCardId);
				psInsert.setString(3,itName);
				psInsert.setString(4,itCollege);
				psInsert.execute();
				return true;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally{
			closeCon(con);
		}
	}
	public static boolean hasIt(String itCardId) {
		
		Connection con=getConnectionUserData();
		try{
			PreparedStatement psCheckExist=con.prepareStatement("select userId from userdata where cardId=?");
			psCheckExist.setString(1,itCardId);
			ResultSet rs=psCheckExist.executeQuery();
			while(rs.next()){
				return true;
			}
			return false;
		}catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		finally{
			closeCon(con);
		}
	}
	public synchronized static int getUserId(){
		int userId=0;
		Connection con=dataBaseConnection.getConnectionUserData();
		try{
			PreparedStatement ps=con.prepareStatement("select * from userdata");
			ResultSet rs=ps.executeQuery();
			for(userId=1;rs.next();++userId){
				userId=rs.getRow();
			}
			return userId;
		}catch (Exception e) {
			e.printStackTrace();
			return userId;
		}
		finally{
			closeCon(con);
		}	
	}
	public static String[] getUserInfo(String cardId) {
		Connection con=dataBaseConnection.getConnectionUserData();
		String[] userInfo=new String[7];
		try{
			PreparedStatement ps=con.prepareStatement("select userName,userCollege,state,grade,remainingTime,paperId,anwser from userdata where cardId=?");
			ps.setString(1,cardId);
			ResultSet rs=ps.executeQuery();
			for(;rs.next();){
				for (int i = 0; i < userInfo.length; i++) {
					userInfo[i]=rs.getString(i+1);
				}
			}
			
			return userInfo;
		}catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		finally{
		closeCon(con);
		}
	}
	public static boolean setPaperId(String itCardId,String itPaperId,String state){
		Connection con=getConnectionUserData();
		boolean flag=false;
		try{
			PreparedStatement ps=con.prepareStatement("update userdata set paperId=?,state=? where cardId=?");
			ps.setString(1,itPaperId);
			ps.setString(2,state);
			ps.setString(3,itCardId);
			ps.execute();
			flag=true;
			return flag;
		}catch (Exception e) {
			e.printStackTrace();
			return flag;
		}
		finally{
		closeCon(con);
		
		}
	}
	public static boolean saveAnswersAndTime(String itAnswer,int remainingSeconds,String itCardId){
		Connection con=getConnectionUserData();
		boolean flag=false;
		try{
			PreparedStatement ps=con.prepareStatement("update userdata set anwser=?,remainingTime=? where cardId=?");
			ps.setString(1,itAnswer);
			ps.setInt(2,remainingSeconds);
			ps.setString(3,itCardId);
			ps.execute();
			flag=true;
			return flag;
		}catch (Exception e) {
			e.printStackTrace();
			return flag;
		}
		finally{
		closeCon(con);
		}
	}
	public static String getPaperId(String cardId){
		Connection con=dataBaseConnection.getConnectionUserData();
		String paperId=new String();
		try{
			PreparedStatement ps=con.prepareStatement("select paperId from userdata where cardId=?");
			ps.setString(1,cardId);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				paperId=rs.getString(1);
			}
			return paperId;
		}catch (Exception e) {
			e.printStackTrace();
			return paperId;
		}
		finally{
			closeCon(con);
		}	
	}
	public static char[] getUserAnswers(String cardId){
		Connection con=dataBaseConnection.getConnectionUserData();
		String userAnswers=new String();
		try{
			PreparedStatement ps=con.prepareStatement("select anwser from userdata where cardId=?");
			ps.setString(1,cardId);
			ResultSet rs=ps.executeQuery();
			while(rs.next()){
				userAnswers=rs.getString(1);
			}
			return userAnswers.toCharArray();
		}catch (Exception e) {
			e.printStackTrace();
			return userAnswers.toCharArray();
		}
		finally{
			closeCon(con);
		}	
	}
	public static boolean finishSave(int grade ,int state,String cardId){
		Connection con=getConnectionUserData();
		boolean flag=false;
		try{
			PreparedStatement ps=con.prepareStatement("update userdata set grade=?,state=? where cardId=?");
			ps.setInt(1,grade);
			ps.setInt(2,state);
			ps.setString(3,cardId);
			ps.execute();
			flag=true;
			return flag;
		}catch (Exception e) {
			e.printStackTrace();
			return flag;
		}
		finally{
		closeCon(con);
		}
	}
		
}
